package com.whately.core.base.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具栏
 */
public class DateUtil {

    public static final String yyyyMM = "yyyyMM";
    public static final String yyyy_MM_dd = "yyyy-MM-dd";
    public static final String yyyyMMdd = "yyyyMMdd",
            yyyyMMddHH = "yyyyMMddHH",
            yyyyMMddHHmm = "yyyyMMddHHmm",
            yyyyMMddHHmmss = "yyyyMMddHHmmss";
    public static final String yyyy_MM_dd_HH_mm_SS = "yyyy-MM-dd HH:mm:ss";   // yyyy-mm-dd  HH:mm:ss
    public static final String yyyy_MM_dd_HH_mm = "yyyy-MM-dd HH:mm";
    public final static String YYYY_MM_DD_DOT="yyyy.MM.dd";

    public static final String yyyy_MM_dd_HH_mm_With_point = "yyyy.MM.dd HH:mm";
    public static final String yyyy_MM_dd__HH_mm_ss_With_point = "yyyy.MM.dd HH:mm:ss";

    /**
     * 当前时间戳的秒数
     * @return  long 秒
     */
    public static long nowSecond() {
        LocalDateTime now=LocalDateTime.now();
        long ts=now.toEpochSecond(ZoneOffset.of("+8"));
        return ts;
    }


    /**
     * 当前时间戳的毫秒数
     * @return  long 毫秒
     */
    public static long nowMilliSecond() {
        return Calendar.getInstance().getTimeInMillis();
    }

    /**
     * 当前时间戳取整点的秒数，小时之后时间被忽略
     * @return  long 秒
     */
    public static long nowSecondOmitAfterHour() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MINUTE,0);
        cal.set(Calendar.SECOND,0);
        cal.set(Calendar.MILLISECOND,0);
        return cal.getTimeInMillis()/1000;
    }

    /**
     * 今天的时间凌晨的时间戳，零点
     * @return
     */
    public static long todaySecondInClock000() {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Long time = calendar.getTimeInMillis() / 1000;
        return time;
    }

    /**
     * 今天的时间午夜的时间戳，23点59分59秒
     * @return
     */
    public static long todaySecondInClock235959() {
        Long time = todaySecondInClock000() + 60*60*24 -1 ;
        return time;
    }

    /**
     * 今天的时间午夜的时间戳，24点
     * @return
     */
    public static long todaySecondInClock24() {
        Long time = todaySecondInClock000() + 60*60*24 ;
        return time;
    }


    /**
     * 获取当前时间的日期格式，格式: 年月 yyyyMM
     */
    public static String nowDate2yyyyMM(){
        Calendar now = Calendar.getInstance();
        String year =now.get(Calendar.YEAR)+"";
        int month = now.get(Calendar.MONTH) + 1;
        String m=null;
        if(10>month){
            m="0"+month;
        }else{
            m=month+"";
        }
        String date = year+m;
        return date;
    }



    /**
     * 获取当前时间的日期格式，格式: 年月 yyyyMMdd
     */
    public static String nowDate2yyyyMMdd(){
        Calendar now = Calendar.getInstance();
        return formatMillis2StringWithPattern(now.getTimeInMillis(),yyyyMMdd);
    }


    /**
     * 获取当前时间往前或者后推xx天的日期格式，格式: 年月 yyyyMM
     * @addOrMinusDay 向前或者向后的天数
     */
    public static String nowDate2yyyyMMdd(int addOrMinusDay){
        Calendar now = Calendar.getInstance();
        now.add(Calendar.DATE, addOrMinusDay);
        return formatMillis2StringWithPattern(now.getTimeInMillis(),yyyyMMdd);
    }

    /**
     * 获取当前时间的日期格式，格式参数指定：pattern
     */
    public static String nowDate2String(String pattern)
    {
        Calendar now = Calendar.getInstance();
        return formatMillis2StringWithPattern(now.getTimeInMillis(),pattern);
    }


    /**
     * 转【秒】为日期字符串，格式参数指定：pattern
     */
    public static String formatSecond2StringWithPattern(Long timeInSecond, String pattern)
    {
        return formatMillis2StringWithPattern(timeInSecond.longValue()*1000,pattern);
    }


    /**
     * 转【毫秒】为日期字符串，格式参数指定：pattern
     */
    public static String formatMillis2StringWithPattern(Long timeInMs, String pattern)
    {
        if (timeInMs == null|| timeInMs==0) {
            return "";
        }
        SimpleDateFormat sdf=new SimpleDateFormat(pattern);
        Calendar c=Calendar.getInstance();
        c.setTimeInMillis(timeInMs);
        return sdf.format(c.getTime());
    }


    /**
     * 将Date类型转换为字符串
     *
     * @param date    日期类型
     * @param pattern 字符串格式
     * @return 日期字符串
     */
    public static String format(Date date, String pattern) {
        if (date == null) {
            return "";
        }
        if (pattern == null || pattern.equals("") || pattern.equals("null")) {
            pattern = yyyy_MM_dd_HH_mm_SS;
        }
        return new SimpleDateFormat(pattern).format(date);
    }

    /**
     * 将pattern类型的字符串转Date
     *
     */
    public static Date getDate(String date, String patten) {
        SimpleDateFormat sf = new SimpleDateFormat(patten);
        try {
            return sf.parse(date);
        } catch (ParseException e) {
        }
        return null;
    }

    /**
     * 根据date获取到秒值
     */
    public static long getDateTimeSecond(Date date){
        long longTime = date.getTime();
        return (int) (longTime / 1000);
    }


    /**
     * 计算当前日期与参数日期之间相差的天数
     * @return int 天数
     */
    public static int daysTillNow(Long seconds){
        if(seconds==null){
            return 0;
        }
        Date dtOld= new Date(seconds*1000);
        String timeOld = DateUtil.format(dtOld,yyyyMMdd);
        String now = DateUtil.format(new Date(),yyyyMMdd);
        if(timeOld.equals(now)){
            return 0;
        }

        long s1=parseCalendarFormat(timeOld,yyyyMMdd).getTimeInMillis()/1000;
        long s2=parseCalendarFormat(now,yyyyMMdd).getTimeInMillis()/1000;
        long l = (s2 -s1)/(3600*24);
        return (int)l;
    }


    /**
     * @param strDate
     * @return java.util.Calendar
     */
    private static Calendar parseCalendarFormat(String strDate) {
        String pattern = yyyy_MM_dd_HH_mm_SS;
        return parseCalendarFormat(strDate, pattern);
    }

    /**
     * @param strDate
     * @param pattern
     * @return java.util.Calendar
     */
    private static Calendar parseCalendarFormat(String strDate, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat();
        Calendar cal = null;
        sdf.applyPattern(pattern);
        try {
            sdf.parse(strDate);
            cal = sdf.getCalendar();
        } catch (Exception e) {
        }
        return cal;
    }



    /**
     * 判断{当前时间}是否在 开始日期跟结束日期之间
     */
    public static boolean between(long startTimeSecond, long endTimeSecond) throws ParseException {
        long nowTsInSecond = nowSecond();
        return between(nowTsInSecond,startTimeSecond,endTimeSecond);
    }

    /**
     * 判断{某个时间}是否在 开始日期跟结束日期之间
     */
    public static boolean between(Long nowTsInSecond, long startTimeSecond, long endTimeSecond) {
        if(nowTsInSecond==null){
            return false;
        }
        return ((nowTsInSecond.compareTo(startTimeSecond) >=0) && (nowTsInSecond.compareTo(endTimeSecond) <=0));
    }

}
